# vim:set ft= ts=4 sw=4 et fdm=marker:

use Test::Nginx::Socket::Lua;
use Dotenv -load => 't/.env';


plan tests => repeat_each() * (blocks() * 3);


$ENV{TEST_NGINX_REDIS_HOST}     ||= '127.0.0.1';
$ENV{TEST_NGINX_REDIS_PORT}     ||= 6379;
$ENV{TEST_NGINX_REDIS_PASSWORD} ||= '';
$ENV{TEST_NGINX_REDIS_DB}         = 0;

our $http_config = <<'_EOC_';
    lua_package_path "${prefix}../../script/?.ljbc;${prefix}../../script/?.lua;;";
_EOC_

no_shuffle();
no_long_string();

run_tests();

__DATA__


=== TEST Casino.PeekTransfer
--- http_config eval: $::http_config
--- config
        location = /t {
            content_by_lua_block {
                local REDIS_SERVER = {
                    host   = "$TEST_NGINX_REDIS_HOST",
                    port   = $TEST_NGINX_REDIS_PORT,
                    secret = "$TEST_NGINX_REDIS_PASSWORD",
                    db     = $TEST_NGINX_REDIS_DB,
                }

                local redis_error_reply = function(err)
                    ngx.log(ngx.ERR, err)
                    ngx.exit(ngx.OK)
                end

                local redis     = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)
                local queue     = require("shared.api.gaas-message-forwarder")
                local forwarder = queue.new(redis, "gaas_test", REDIS_SERVER.db)

                -- setup test case
                do
                    -- ignored
                end

                -- perform response
                do
                    local ljson  = require "shared.test.ljson"

                    local res, err = forwarder:write("my_demo_stream",
                                        {
                                            field1 = "value1",
                                            field2 = "value2",
                                        })

                    ngx.header.content_type = 'application/json; charset=utf-8'
                    ngx.print(ljson.encode({
                        res = res,
                        err = err
                    }))
                end

                -- teardown test case
                do
                    require("shared.test.redis-test").teardown_test_case(redis, $TEST_NGINX_REDIS_DB)
                end
            }
        }
--- request
GET /t
--- response_body_like chomp
^\{"res":"\d+-\d+"}$
--- error_code: 200
--- no_error_log
[error]


